﻿using System;
using System.Linq;
using System.Web.UI;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;

namespace System.Web
{
    public static partial class Extensions
    {
        #region PreValidate
        internal const int UploadImageMaxLength = 1024 * 1024;
        internal static readonly string[] UploadImageFileExt;

        static Extensions()
        {
            UploadImageFileExt = new string[] { ".jpg", ".gif", ".png" };
        }

        public static bool PreImageValidate(this HttpPostedFile postedFile, out string errorMessage)
        {
            if (postedFile.ContentLength == 0)
            {
                errorMessage = "请选择一张非空的图片";
                return false;
            }
            if (postedFile.ContentLength > UploadImageMaxLength)
            {
                errorMessage = "图片文件大小超出限制";
                return false;
            }
            string ext = Path.GetExtension(postedFile.FileName);
            if (!UploadImageFileExt.Any(t => string.Compare(t, ext, true) == 0))
            {
                errorMessage = "图片文件类型不在接受范围内";
                return false;
            }
            errorMessage = string.Empty;
            return true;
        }
        #endregion

        #region SaveImageAs
        public static string SaveImageAs(this HttpPostedFile postedFile, CreateFolderMode mode, string savePath)
        {
            string vrtualFilePath = null;
            byte[] buffer = new byte[postedFile.ContentLength];
            postedFile.InputStream.Read(buffer, 0, buffer.Length);
            if (FileValidator.ValidateImage(buffer))
            {
                vrtualFilePath = App.GetFilePath(mode, savePath);
                HttpContext context = HttpContext.Current;
                string physicalFilePath = context.Server.MapPath(vrtualFilePath);
                App.CreateDirectory(physicalFilePath);
                File.WriteAllBytes(physicalFilePath, buffer);
            }
            return vrtualFilePath;
        }

        public static string[] SaveImageAs(this HttpPostedFile postedFile, CreateFolderMode mode, WaterMarkSetting markSetting, params ThumbnailSetting[] thumSettings)
        {
            string[] vrtualPaths = new string[1 + thumSettings.Length];
            byte[] buffer = new byte[postedFile.ContentLength];
            postedFile.InputStream.Read(buffer, 0, buffer.Length);
            if (FileValidator.ValidateImage(buffer))
            {
                string srcPath = string.Format("{0}{1}.{2}", GDIHelper.TempPath, Guid.NewGuid().ToString(), Path.GetExtension(postedFile.FileName));
                postedFile.SaveAs(srcPath);
                HttpContext context = HttpContext.Current;
                string filename = string.Format("{0}.{1}", StringHelper.NowDateString, Path.GetExtension(postedFile.FileName));
                string vitualPath = App.GetFilePath(mode, string.Format("{0}/{1}", markSetting.SavePath, filename));
                markSetting.SavePath = context.Server.MapPath(vitualPath);
                GDIHelper.MakeWaterMark(srcPath, markSetting);
                vrtualPaths[0] = vitualPath;
                for (int i = 0; i < thumSettings.Length; )
                {
                    var thumSetting = thumSettings[i];
                    vitualPath = App.GetFilePath(mode, string.Format("{0}/{1}", thumSetting.SavePath, filename));
                    thumSetting.SavePath = context.Server.MapPath(vitualPath);
                    GDIHelper.MakeThumbnail(srcPath, thumSetting);
                    vrtualPaths[++i] = vitualPath;
                }
            }
            return vrtualPaths;
        }
        #endregion
    }
}